package com.store.bookstore_backend.handler;

import com.store.bookstore_backend.dto.APIResponse;
import com.store.bookstore_backend.exception.BookStoreException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHanlder {
    @ExceptionHandler
    public APIResponse<Object> handleException(Exception ex) {
        if (ex instanceof BookStoreException) {
            BookStoreException bookStoreException = (BookStoreException) ex;
            log.error("出现了异常!!!", ex);
            return APIResponse.error(bookStoreException.getCode(), bookStoreException.getMessage());
        } else {
            log.error("出现了异常!!!", ex);
            return APIResponse.error(BookStoreException.CodeMsgEnum.ERROR);
        }
    }
}
